package com.wg.photo.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.wg.photo.ABSOnItemClickListener;
import com.wg.photo.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Went_Gone on 2017/5/15.
 */

public class RVChoosePictureGlideStringAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "RVChoosePictureAdapter";
    private List<String> mPath;
    private List<String> selectPath;
    private ContentResolver cr;
    private Context mContext;
    private RequestManager manager;
    private Handler handler = new Handler(Looper.getMainLooper());
    private boolean showCamera = true;
    private ABSOnItemClickListener absOnItemClickListener;

    public boolean isShowCamera() {
        return showCamera;
    }

    public void setAbsOnItemClickListener(ABSOnItemClickListener absOnItemClickListener) {
        this.absOnItemClickListener = absOnItemClickListener;
    }

    public RVChoosePictureGlideStringAdapter(Context context,List<String> mPath, RequestManager manager) {
        this.mContext = context;
        this.mPath = mPath;
        this.manager = manager;
        this.selectPath = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (showCamera){
            if (position == 0){
                return 2;
            }else {
                return 1;
            }
        }else {
            return 1;
        }
    }

    public String getItem(int position){
        return mPath.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case 1:
                holder =  new RVChoosePictureViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_picture_glide,parent,false));
                break;
            case 2:
                holder =  new RVChoosePictureViewHolderAdd(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_picture_glide_add,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//        ((RVChoosePictureViewHolder)holder).sdv.setImageURI("file://"+mPath.get(position));
     /*   if (holder instanceof RVChoosePictureViewHolder){
            ((RVChoosePictureViewHolder) holder).bindView(mPath.get(showCamera?position-1:position));

        }*/


        if (holder instanceof RVChoosePictureViewHolder) {
            String path;
            if (showCamera) {
                path = mPath.get(position - 1);
            } else {
                path = mPath.get(position);
            }
            Log.e(TAG, showCamera+"onBindViewHolder: "+position+"||"+path);
            RVChoosePictureViewHolder gridImageViewHolder = (RVChoosePictureViewHolder) holder;
            gridImageViewHolder.bindView(path);
            ((RVChoosePictureViewHolder) holder).viewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (absOnItemClickListener!=null){
                        absOnItemClickListener.onItemClick(v,showCamera?holder.getAdapterPosition()-1:holder.getAdapterPosition());
                    }
                }
            });
            ((RVChoosePictureViewHolder) holder).ivSelected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (absOnItemClickListener!=null){
                        absOnItemClickListener.selected(showCamera?holder.getAdapterPosition()-1:holder.getAdapterPosition());
                    }
                }
            });
        }
    }

    public void updateData(List<String> paths) {
        mPath = paths;
        selectPath.clear();
        notifyDataSetChanged();
    }
    public List<String> getSelectPath() {
        return selectPath;
    }

    public void setSelectPath(List<String> selectPath) {
        this.selectPath = selectPath;
    }

    public boolean selectPath(String path){
        if (selectPath.contains(path)){
            selectPath.remove(path);
        }else {
            if (selectPath.size()>=9){
                return false;
            }else {
                selectPath.add(path);
            }
        }
        return true;
    }


    @Override
    public int getItemCount() {
        if (showCamera){
            return mPath==null?0:mPath.size()+1;
        }else {
            return mPath==null?0:mPath.size();
        }
    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        if(holder instanceof RVChoosePictureViewHolder) {
            RVChoosePictureViewHolder gridImageViewHolder = (RVChoosePictureViewHolder) holder;
            Glide.clear(gridImageViewHolder.weekImage);
        }
        super.onViewRecycled(holder);
    }

    private class RVChoosePictureViewHolder extends RecyclerView.ViewHolder{
        View viewRoot;
        ImageView weekImage,ivSelected;
        public RVChoosePictureViewHolder(View itemView) {
            super(itemView);
            ImageView imageView = (ImageView) itemView.findViewById(R.id.item_choosepicture_IV);
            viewRoot = itemView.findViewById(R.id.item_choosepicture_root);
            ivSelected = (ImageView) itemView.findViewById(R.id.item_choosepicture_IV_select);

            final WeakReference<ImageView> imageViewWeakReference = new WeakReference<>(imageView);
            weekImage = imageViewWeakReference.get();
        }

        private void bindView(final String path){
            if (selectPath.contains(path)) {
                select();
            } else {
                unSelect();
            }
            if (weekImage!=null){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        manager
                                .load(Uri.parse("file://" + path))
                                .dontAnimate()
                                .crossFade()
                                .into(weekImage);
                    }
                });
                weekImage.setTag(R.id.item_choosepicture_IV, path);
            }
        }
        void select() {
            ivSelected.setImageResource(R.mipmap.single_dialog_check_selected);
            ivSelected.setTag(R.id.item_choosepicture_IV_select, true);
        }

        void unSelect() {
            ivSelected.setImageResource(R.mipmap.single_dialog_check_normal);
            ivSelected.setTag(R.id.item_choosepicture_IV_select, false);
        }
    }

    private class RVChoosePictureViewHolderAdd extends RecyclerView.ViewHolder{
        View viewRoot;
        public RVChoosePictureViewHolderAdd(View itemView) {
            super(itemView);
            viewRoot = itemView.findViewById(R.id.item_choosepicture_root);
            viewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (absOnItemClickListener!=null){
                        absOnItemClickListener.add();
                    }
                }
            });
        }
    }
}
